<template>
  <view class="page-wrap">
    <view v-if="havebinded" class="verify-wrapper">
      <p class="title">您绑定的企业</p>
      <view class="item-list">
        <view class="enterprise-binded">
          <view class="bind-item">
            <span>{{ bindedSite.siteDO.name }}</span>
          </view>
          <view class="bind-item bind-item-phone">
            <span>您已绑定手机号</span>
            <span>{{ bindedSite.phone }}</span>
          </view>
        </view>
      </view>
      <view class="btn-wxphone bgFCAF4F mt140" @click="changeBind">
        点此换绑
      </view>
    </view>
    <view v-if="showWXVisible" class="welcome-wrapper">
      <image
        src="/authorize/img/chatu.png"
        mode="widthFix"
      />
      <view class="wel-tip">欢迎使用{{ settings.name }}员工自助服务，点击下方按钮验证身份现在就加入您所在的企业吧！</view>
      <view class="use-tip">使用之前需要通过手机号验证所属企业</view>
      <button class="btn-wxphone bgFCAF4F" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信手机号快速验证</button>
      <view class="btn-wxphone colorFCAF4F" @click="showPhoneVerfify">
        输入手机号验证
      </view>
    </view>
    <!-- <view v-if="showPhoneVisible" class="welcome-wrapper">
      <view class="title">手机号验证</view>
      <view class="sign-form">
        <view class="input-box">
          <input
            v-model="phone"
            placeholder="输入手机号"
            placeholder-class="placeholder"
            class="input"
            type="number"
          >
        </view>
        <view class="input-box">
          <input
            v-model="code"
            class="input"
            placeholder="输入验证码"
            placeholder-class="placeholder"
            type="text"
          >
          <button
            :disabled="disabledGetVerificationCode"
            class="input-button-mini"
            @click="getVerificationCode"
          >{{ showTimmer ? second :text }}</button>
        </view>
      </view>
      <view class="btn-wxphone bgFCAF4F" @click="goVerfify">
        立即验证
      </view>
    </view> -->
  </view>
</template>
<script>
import { checkBindByUnionId, getPhoneNumber } from '@/services/auth'
import { checkPhone } from "@/utils/validate"
import { sendCodeReq, checkHaveBind } from '@/services/register'
import { mapState } from "vuex"
export default {
  name: '',
  props: {},
  data(){
    return {
      login_code: '',
      sites: [],
      bindedSite: {},
      phone: "",
      code: "",
      currentSite: null,
      showWXVisible: false,
      showPhoneVisible: false,
      showTimmer: false,
      second: 60,
      text: "获取验证码",
      disabledGetVerificationCode: false,
      disabledSubmit: false,
      havebinded: false,
      sealString: '',
      unionId: '',
      openId: ''
    }
  },
  computed: {
    ...mapState(['settings'])
  },
  onLoad: function(options) {
    uni.hideHomeButton()
    this.unionId = options.unionId
    this.openId = options.openId
    this.checkBindByUnionId(options.unionId)
    var that = this;
    uni.login({
      success: function(res) {
        // 获取code
        that.login_code = res.code;
      }
    })
  },
  mounted(){},
  methods: {
    checkBindByUnionId(unionId){
      checkBindByUnionId({ unionId: unionId }).then(res => {
        this.bindedSite = res
        if(res.bindStatus === 1){
          this.havebinded = true
        }else{
          this.showWXVisible = true
          this.havebinded = false
        }
      })
    },
    showPhoneVerfify(){
      // this.havebinded = false
      // this.showWXVisible = false
      // this.showPhoneVisible = true
      uni.navigateTo({
        url: '/authorize/phoneverify?phone=' + this.phone + '&unionId=' + this.unionId + '&openId=' + this.openId
      })
    },
    changeBind(){
      this.havebinded = false
      this.showWXVisible = true
    },
    getPhoneNumber(e){
      var that = this;
      if (e.target.errMsg === "getPhoneNumber:ok") { // 同意授权
        uni.hideLoading()
        if(e.target && e.target['iv'] && e.target['encryptedData']){
          const data = {
            jsCode: that.login_code,
            iv: e.target.iv,
            encryptedData: e.target.encryptedData,
            terminalType: 'B'
          }
          getPhoneNumber(data).then(res => {
            that.phoneNumber = res.phoneNumber
            // 验证手机号
            checkHaveBind({ phone: res.phoneNumber, unionId: that.unionId }).then(res => {
              if(res === true){
                uni.redirectTo({
                  url: '/authorize/join?phone=' + that.phoneNumber + '&unionId=' + that.unionId + '&openId=' + that.openId
                })
              }else{
                uni.showModal({
                  title: '提示',
                  content: res,
                  success: () => {}
                });
              }
            })
          }).catch(err => {
            // 弹框提示
            uni.showToast({
              title: err,
              icon: 'none',
              duration: 2000
            })
          })
        }else{
          uni.showToast({
            title: '微信授权失败',
            icon: 'none',
            duration: 2000
          })
        }
      } else {
        // 拒绝手机号授权
        uni.showToast({
          title: '手机号授权失败',
          icon: 'none',
          duration: 2000
        });
      }
    },
    // checkSession() {
    //   return new Promise((resolve, reject) => {
    //     wx.checkSession({
    //       success(res) {
    //         // session_key 未过期，并且在本生命周期一直有效
    //         const code = uni.getStorageSync('code') || '';
    //         // TODO:code==‘’
    //         resolve(code);
    //       },
    //       fail: async(err) => {
    //         // session_key 已经失效，需要重新执行登录流程
    //         const code = await onLogin();
    //         reject(code);
    //       }
    //     })
    //   })
    // },
    sendCode(){
      sendCodeReq(this.phone).then(() => {
        uni.showToast({
          title: '验证码发送成功',
          icon: 'none',
          duration: 2000
        })
        this.showTimmer = true
        this.disabledGetVerificationCode = true
        const timer = setInterval(() => {
          this.second = this.second - 1
          if (this.second === 0) {
            this.disabledGetVerificationCode = false
            this.showTimmer = false
            this.second = 60
            this.text = "重新获取"
            clearInterval(timer)
          }
        }, 1000)
      }).catch((err) => {
        uni.showToast({
          title: err.message,
          icon: "none",
          duration: 2000
        })
      })
    },
    getVerificationCode() {
      // 手机号格式校验
      if (checkPhone(this.phone)) {
        this.sendCode()
      } else {
        uni.showToast({
          title: "手机号格式错误",
          icon: "none",
          duration: 2000
        })
      }
    },
    goBack(){
      uni.redirectTo({
        url: '/pages/index/index'
      })
    }
  }
}
</script>
